<?php
namespace Admin\Controller;
use Think\Controller;
class DrawController extends PublicController{

    /**
     * 抽奖物品列表
     */
	public function index() {
	    $list = M("award")->where('del=0')->order("addtime DESC")->select();
        $this->assign('list',$list);
        $this->display();
	}

    /**
     * 抽奖物品的修改添加
     * @author tiedong
     * @date 2017-01-16
     */
	public function add(){
	    $award_model = D("award");
		$id = intval($_GET['id']);
		if ($id>0) {
			$award = $award_model->where('id='.intval($id).' AND del=0')->find();
			$this->assign('voucher',$award);
		}

		$this->display();
	}

    /**
     * 抽奖物品的修改添加
     * @author tiedong
     * @date 2017-01-16
     */
	public function save(){
        $award_model = D("award");
		$id          = intval($_POST['id']);
		$type        = intval($_POST['type']);
		$probability = floatval($_POST['probability']);
		$desc        = htmlspecialchars($_POST['desc']);
        $res         =  $award_model->check_probability($id);
        if ( ($res+$probability)>100) {
            $this->error("概率已超过100%，请重新修改");
        }
        if( $type==1)  {
            $point = intval($_POST['point']);
            if (!$point) {
                $this->error('请输入积分.'.__LINE__);
                exit();
            }
        }else{
            $point = 0;
        }
        if (!$probability) {
            $this->error('中奖概率不能为空.'.__LINE__);
            exit();
        }
		if (!$desc) {
			$this->error('描述不能为空.');
			exit();
		}
		$data = array();
		$data['type']        = $type;
		$data['point']       = $point;
		$data['probability'] = $probability;
		$data['desc']        = $desc;
        //上传图片
        if (!empty($_FILES["award_file"]["tmp_name"])) {
            //文件上传
            $info = $this->upload_images($_FILES["award_file"],array('jpg','png','jpeg'),"award/".date(Ymd));
            if(!is_array($info)) {
                // 上传错误提示错误信息
                $this->error($info);
                exit();
            }else{
                // 上传成功 获取上传文件信息
                $data['img'] = 'UploadFiles/'.$info['savepath'].$info['savename'];
                $xt = $award_model->where('id='.intval($id))->field('img')->find();
                if ($id && $xt['img']) {
                    $img_url = "Data/".$xt['img'];
                    if(file_exists($img_url)) {
                        @unlink($img_url);
                    }
                }
            }
        }
		if ($id>0) {
			$res = $award_model->where('id='.intval($id))->save($data);
		}else{
			$data['addtime'] = time();
			$res = $award_model->add($data);
		}
		if ($res) {
			$this->success('操作成功！','index');
			exit();
		}else{
			$this->error('操作失败！');
			exit();
		}
	}

    /**
     * 抽奖物品的删除
     * @author tiedong
     * @date 2018-01-16
     */
	public function del(){
		$id = intval($_GET['did']);
		$check_info = M('award')->where('id='.intval($id))->find();
		if (!$check_info) {
			echo '<script>alert("参数错误.");history.go(-1);</script>';
			exit();
		}
		//判断该奖项是否已经删除
		if (intval($check_info['del'])==1) {
			$this->success('操作成功！'.__LINE__,'index');
			exit();
		}

		$up = M('award')->where('id='.intval($id))->save(array('del'=>1));
		if ($up) {
			$this->success('操作成功！','index');
			exit();
		}else{
			echo '<script>alert("操作失败.");history.go(-1);</script>';
		    exit;
		}
	}

	/**
     * 获奖用户列表
     * @author tiedong
     * @date 2018-02-09
     */
    public function awardList()
    {
        define('rows',20);
        $count=M("user_award")->count();
        $rows=ceil($count/rows);
        $page=(int)$_GET['page'];
        $page<0?$page=0:'';
        $limit=$page*rows;
        $type = ['1'=>'积分','0'=>'物品'];
        $status = ['1'=>'已兑换','2'=>'尚未兑换'];
        $list = M("user_award")->alias("a")
                    ->join(" lr_user u on u.id = a.uid")
                    ->join( " lr_award c on c.id = a.a_id ")
                    ->order( " a.addtime desc")
                    ->field("a.id,a.addtime,u.name,c.title,a.type,a.status,a.post")
                    ->limit($limit,rows)
                    ->select();
        $page_index=$this->page_index($count,$rows,$page);
        foreach ( $list as $key => $value ) {
            $list[$key]['type'] = $type[$value['type']];
            $list[$key]['status'] = $status[$value['status']];
            if ( !empty($value['post']) ) {
                $list[$key]['ff'] = 1;
            } else {
                $list[$key]['ff'] = 0;
            }
        }
        $this->assign('page_index',$page_index);
        $this->assign("list",$list);
        $this->display();
	}

	/**
     * 查看详情
     * @author tiedong
     * @date 2018-02-06
     */
    public function awardDetail()
    {
        if ( IS_POST ) {
            $id = I("post.id");
            $status = M("user_award")->where('id='.$id)->getField("status");
            if ( $status==2 ) {
                $this->error("用户尚未兑换");
                exit;
            }
            if ( empty(I("post.kuaidi_name") || empty(I("post.kuaidi_num")))) {
                $this->error("请输入快递名称或快递单号");
                exit;
            }
            $data = ['post'=>I("post.kuaidi_name"),'kuaidi_num'=>I("post.kuaidi_num")];
            $result = M("user_award")->where("id=".$id)->save($data);
            if ( $result ) {
                $this->success('操作成功',U("awardList"));
            } else {
                $this->error("操作失败！");
            }

        } else {
            $id = !empty($_GET['id']) ? intval($_GET['id']) : null ;
            if ( empty($id) ) {
                $this->error("参数有误！");
            }
            //获取物流信息
            $wuliu = $this->getLogistics();
            $this->assign("wuliu",$wuliu);
            $info = M("user_award")->alias("a")
                ->join(" lr_user u on u.id = a.uid")
                ->join( " lr_award c on c.id = a.a_id ")
                ->where("a.id=".$id)
                ->order( " a.addtime desc")
                ->field("a.id,a.addtime,u.name,c.desc,c.title,a.type,a.status,a.receiver,a.tel,a.address_xq,a.kuaidi_num,a.post")
                ->find();
            $this->assign("info",$info);
            $this->display();
        }

	}


    public function getLogistics()
    {
        $wuliu = C("WULIU");
        $wuliu_arr = array();
        foreach ( $wuliu as $key=>$value ) {
            $wuliu_arr[$key] = $value[0];
        }
        // var_dump($wuliu_arr);
        return $wuliu_arr;
    }


}